using HKB.Common;
using System;
using System.Data;
using System.Text;
using System.Web;
namespace HKB.Web
{
	public class Shop_Article
	{
		private static Shop_Article _instance = null;
		private Conn conn = null;
		private Pagination _pn;
		public static Shop_Article instance
		{
			get
			{
				if (Shop_Article._instance == null)
				{
					Shop_Article._instance = new Shop_Article(new Conn());
				}
				return Shop_Article._instance;
			}
		}
		protected Pagination pn
		{
			get
			{
				Pagination pn;
				if (this._pn == null)
				{
					this._pn = new Pagination();
					this._pn.SQL_LeftJoinTable = "HLShop_Module";
					this._pn.SQL_LeftJoinField = "Title";
					this._pn.SQL_JoinField = "id";
					this._pn.SQL_JoinField2 = "ChannelID";
					this._pn.P_Object = "个" + Language.GetAdminMsg("Article/Title");
					pn = this._pn;
				}
				else
				{
					pn = this._pn;
				}
				return pn;
			}
		}
		public Shop_Article(Conn _conn)
		{
			this.conn = _conn;
		}
		public Shop_Article()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public DataTable GetAllArticle(int _page, string WhereStr, int pagesize)
		{
			return this.pn.GetDataTable("HLShop_IndexArticle", _page, pagesize, "ArticleID", WhereStr, "ArticleID Desc", this.conn, null);
		}
		public string ShowPagePanel(string pagelinkhead)
		{
			string result;
			if (this.pn != null)
			{
				this.pn.P_LinkHead = pagelinkhead;
				string text = this.pn.ShowPagePanel();
				result = text;
			}
			else
			{
				result = "";
			}
			return result;
		}
		public DataTable GetOnTopIndexArticleByClassID(int ClassID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ClassID", DbType.Int32, ClassID);
			return this.conn.GetDataTable("SELECT * FROM [HLShop_IndexArticle] WHERE (ClassID = @ClassID) AND (OnTop = 1) AND (Status = 1) ORDER BY ArticleID DESC", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public bool AddNewArticle(int ChannelID, int ClassID, string ArticleType, int TitleType, string ShortTitle, string LognTitle, string ArticleContent, string LinkUrl, int TitleLength, string PicUrl, DateTime InputTime, int Hits, string Editor, int OnTop, int Elite, int Validay, int Status, int SpecialID, string UploadPicUrl, int IsHighDisplay, int LinkType, string TemplatePath)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@ChannelID", DbType.Int32, ChannelID),
				this.conn.CreateParameter("@ClassID", DbType.Int32, ClassID),
				this.conn.CreateParameter("@ArticleType", DbType.String, ArticleType),
				this.conn.CreateParameter("@TitleType", DbType.Int32, TitleType),
				this.conn.CreateParameter("@ShortTitle", DbType.String, ShortTitle),
				this.conn.CreateParameter("@LognTitle", DbType.String, LognTitle),
				this.conn.CreateParameter("@ArticleContent", DbType.String, ArticleContent),
				this.conn.CreateParameter("@LinkUrl", DbType.String, LinkUrl),
				this.conn.CreateParameter("@TitleLength", DbType.Int32, TitleLength),
				this.conn.CreateParameter("@PicUrl", DbType.String, PicUrl),
				this.conn.CreateParameter("@InputTime", DbType.DateTime, InputTime),
				this.conn.CreateParameter("@Hits", DbType.Int32, Hits),
				this.conn.CreateParameter("@Editor", DbType.String, Editor),
				this.conn.CreateParameter("@OnTop", DbType.Int32, OnTop),
				this.conn.CreateParameter("@Elite", DbType.Int32, Elite),
				this.conn.CreateParameter("@Validay", DbType.Int32, Validay),
				this.conn.CreateParameter("@Status", DbType.Int32, Status),
				this.conn.CreateParameter("@SpecialID", DbType.Int32, SpecialID),
				this.conn.CreateParameter("@UploadPicUrl", DbType.String, UploadPicUrl),
				this.conn.CreateParameter("@IsHighDisplay", DbType.Int32, IsHighDisplay),
				this.conn.CreateParameter("@LinkType", DbType.Int32, LinkType),
				this.conn.CreateParameter("@TemplatePath", DbType.String, TemplatePath)
			};
			int num = this.conn.Execute("INSERT INTO [HLShop_IndexArticle] ([ChannelID], [ClassID], [ArticleType], [TitleType], [ShortTitle], [LognTitle], [ArticleContent], [LinkUrl], [TitleLength], [PicUrl], [IncludePic], [InputTime], [UpdateTime], [Hits], [Editor], [Elite], [ArticleUserID], [Validay], [Status], [SpecialID], [OnTop], [UploadPicUrl], [IsHighDisplay], [LinkType], [TemplatePath]) VALUES (@ChannelID, @ClassID, @ArticleType, @TitleType, @ShortTitle, @LognTitle, @ArticleContent, @LinkUrl, @TitleLength, @PicUrl, 0, @InputTime, '" + DateTime.Now.ToString() + "', @Hits, @Editor, @Elite, 0, @Validay, @Status, @SpecialID, @OnTop, @UploadPicUrl, @IsHighDisplay, @LinkType, @TemplatePath)", parameters);
			return num == 1;
		}
		public DataTable GetIndexArticle(string ArticleType)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ArticleType", DbType.String, ArticleType);
			return this.conn.GetDataTable("SELECT * FROM HLShop_IndexArticle WHERE ArticleType=@ArticleType AND OnTop=1 AND Elite=1 ORDER BY ArticleID DESC", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetArticleByTypeSpecialID(int SpecialID, string ArticleType)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@SpecialID", DbType.Int32, SpecialID),
				this.conn.CreateParameter("@ArticleType", DbType.String, ArticleType)
			};
			return this.conn.GetDataTable("SELECT * FROM HLShop_IndexArticle WHERE SpecialID = @SpecialID and ArticleType=@ArticleType AND OnTop=1 AND Elite=1 ORDER BY ArticleID DESC", parameters);
		}
		public DataTable GetArticleByArticleID(int ArticleID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ArticleID", DbType.Int32, ArticleID);
			return this.conn.GetDataTable("SELECT * FROM HLShop_IndexArticle WHERE ArticleID = @ArticleID", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetArticlePrevAndNextByArticleId(int ArticleID, bool isPrev)
		{
			string str = string.Empty;
			if (isPrev)
			{
				str = "WHERE ArticleID < " + ArticleID + " ORDER BY ArticleID DESC";
			}
			else
			{
				str = "WHERE ArticleID > " + ArticleID + " ORDER BY ArticleID ASC";
			}
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ArticleID", DbType.Int32, ArticleID);
			return this.conn.GetDataTable("SELECT top 1 ArticleID,ShortTitle FROM HLShop_IndexArticle " + str);
		}
		public bool UpdateArticle(int ArticleID, int ChannelID, int ClassID, string ArticleType, int TitleType, string ShortTitle, string LognTitle, string ArticleContent, string LinkUrl, int TitleLength, string PicUrl, int Hits, string Editor, int OnTop, int Elite, int Validay, int Status, int SpecialID, DateTime UpdateTime, string UploadPicUrl, int IsHighDisplay, int LinkType, string TemplatePath)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@ChannelID", DbType.Int32, ChannelID),
				this.conn.CreateParameter("@ClassID", DbType.Int32, ClassID),
				this.conn.CreateParameter("@ArticleType", DbType.String, ArticleType),
				this.conn.CreateParameter("@TitleType", DbType.Int32, TitleType),
				this.conn.CreateParameter("@ShortTitle", DbType.String, ShortTitle),
				this.conn.CreateParameter("@LognTitle", DbType.String, LognTitle),
				this.conn.CreateParameter("@ArticleContent", DbType.String, ArticleContent),
				this.conn.CreateParameter("@LinkUrl", DbType.String, LinkUrl),
				this.conn.CreateParameter("@TitleLength", DbType.Int32, TitleLength),
				this.conn.CreateParameter("@PicUrl", DbType.String, PicUrl),
				this.conn.CreateParameter("@UpdateTime", DbType.DateTime, UpdateTime),
				this.conn.CreateParameter("@Hits", DbType.Int32, Hits),
				this.conn.CreateParameter("@Editor", DbType.String, Editor),
				this.conn.CreateParameter("@OnTop", DbType.Int32, OnTop),
				this.conn.CreateParameter("@Elite", DbType.Int32, Elite),
				this.conn.CreateParameter("@Validay", DbType.Int32, Validay),
				this.conn.CreateParameter("@Status", DbType.Int32, Status),
				this.conn.CreateParameter("@SpecialID", DbType.Int32, SpecialID),
				this.conn.CreateParameter("@UploadPicUrl", DbType.String, UploadPicUrl),
				this.conn.CreateParameter("@IsHighDisplay", DbType.Int32, IsHighDisplay),
				this.conn.CreateParameter("@LinkType", DbType.Int32, LinkType),
				this.conn.CreateParameter("@TemplatePath", DbType.String, TemplatePath),
				this.conn.CreateParameter("@Original_ArticleID", DbType.String, ArticleID)
			};
			int num = this.conn.Execute("UPDATE [HLShop_IndexArticle] SET [ChannelID] = @ChannelID, [ClassID] = @ClassID, [ArticleType] = @ArticleType, [TitleType] = @TitleType, [ShortTitle] = @ShortTitle, [LognTitle] = @LognTitle, [ArticleContent] = @ArticleContent, [LinkUrl] = @LinkUrl, [TitleLength] = @TitleLength, [PicUrl] = @PicUrl, [UpdateTime] = @UpdateTime, [Hits] = @Hits, [Editor] = @Editor, [Elite] = @Elite, [Validay] = @Validay, [Status] = @Status, [SpecialID] = @SpecialID, [OnTop] = @OnTop, [UploadPicUrl] = @UploadPicUrl, [IsHighDisplay] = @IsHighDisplay, [LinkType] = @LinkType, [TemplatePath] = @TemplatePath WHERE (([ArticleID] = @Original_ArticleID))", parameters);
			return num == 1;
		}
		public bool EditArticle(string Type, string ArticleIDArray)
		{
			string text = "";
			bool result;
			if (ArticleIDArray != "")
			{
				switch (Type)
				{
				case "delete":
					text = "delete From HLShop_IndexArticle";
					break;
				case "ontop":
					text = "update HLShop_IndexArticle Set OnTop=1";
					break;
				case "cancleontop":
					text = "update HLShop_IndexArticle Set OnTop=0";
					break;
				case "iselite":
					text = "update HLShop_IndexArticle Set Elite=1";
					break;
				case "cancleelite":
					text = "update HLShop_IndexArticle Set Elite=0";
					break;
				case "pass":
					text = "update HLShop_IndexArticle Set Status=1";
					break;
				case "nopass":
					text = "update HLShop_IndexArticle Set Status=-1";
					break;
				}
				text = text + " WHERE ArticleID in(" + ArticleIDArray + ")";
				int num2 = this.conn.Execute(text);
				result = (num2 > 0);
			}
			else
			{
				result = false;
			}
			return result;
		}
		public DataTable GetArticleByModuleID(int ModuleID)
		{
			DataTable result;
			if (ModuleID > 0)
			{
				IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ModuleID", DbType.Int32, ModuleID);
				result = this.conn.GetDataTable("SELECT * FROM HLShop_IndexArticle WHERE ChannelID = @ModuleID", new IDbDataParameter[]
				{
					dbDataParameter
				});
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataTable GetArticleByModuleID2(int ModuleID, int selectcount)
		{
			DataTable result;
			if (ModuleID > 0)
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.Append("SELECT ");
				if (selectcount > 0)
				{
					stringBuilder.AppendFormat(" top {0} ", selectcount);
				}
				stringBuilder.Append(" ArticleID,");
				stringBuilder.Append(" case when isnull(ShortTitle,'')='' then LognTitle else ShortTitle end ");
				stringBuilder.Append(" as  Title ");
				stringBuilder.Append(" from  dbo.HLShop_IndexArticle ");
				stringBuilder.AppendFormat(" where ChannelID={0} ", ModuleID);
				stringBuilder.Append(" and datediff(day,dateadd(day,Validay,InputTime),GetDate())<=0 ");
				stringBuilder.Append(" and [Status]=1 ");
				stringBuilder.Append(" order by OnTop,UpdateTime desc ");
				result = this.conn.GetDataTable(stringBuilder.ToString());
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataTable GetArticleByModuleID(int ModuleID, int SpeicalID)
		{
			DataTable result;
			if (ModuleID > 0)
			{
				IDbDataParameter[] parameters = new IDbDataParameter[]
				{
					this.conn.CreateParameter("@ModuleID", DbType.Int32, ModuleID),
					this.conn.CreateParameter("@SpecialID", DbType.Int32, SpeicalID)
				};
				result = this.conn.GetDataTable("SELECT * FROM HLShop_IndexArticle WHERE ChannelID=@ModuleID and SpecialID = @SpecialID ORDER BY ArticleID DESC", parameters);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public void UpdateHits(int ArticleID)
		{
			if (ArticleID > 0)
			{
				IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ArticleID", DbType.Int32, ArticleID);
				this.conn.Execute("UPDATE [HLShop_IndexArticle] Set Hits=Hits+1 WHERE ArticleID = @ArticleID", new IDbDataParameter[]
				{
					dbDataParameter
				});
			}
		}
		public DataTable SearchArticle(int PageSize, int PageIndex, string ModuleID, string ClassID, string ArticelDateBegin, string ArticlDateEnd, int SortType, string OtherType, string ArticleType, string Editor, int LinkType, bool Summary)
		{
			string text = "";
			if (PageSize > 0)
			{
			}
			if (ModuleID != "" && ModuleID != "-1")
			{
				if (Text.IsNumber(ModuleID))
				{
					text = text + " (ChannelID=" + ModuleID + ") AND";
				}
			}
			if (ClassID != "" && ClassID != null && ClassID != "-1")
			{
				if (Text.IsNumber(ClassID))
				{
					text = text + " (ClassID=" + ClassID + ") AND";
				}
			}
			string text2 = Text.ReplaceBadChar(ArticelDateBegin);
			string text3 = Text.ReplaceBadChar(ArticlDateEnd);
			if (text2 != null && text2 != "" && text3 != null && text3 != "")
			{
				string text4 = text;
				text = string.Concat(new string[]
				{
					text4,
					" (InputTime>",
					text2,
					" AND InputTime<",
					text3,
					" ) AND"
				});
			}
			else
			{
				if (text2 != null && text2 != "")
				{
					text = text + " (InputTime>" + text2 + " ) AND";
				}
				else
				{
					if (text3 != null && text3 != "")
					{
						text = text + " (InputTime<" + text3 + " ) AND";
					}
				}
			}
			if (OtherType != "" && OtherType != null)
			{
				string text5 = OtherType.ToLower();
				if (text5 != null)
				{
					if (!(text5 == "ontop"))
					{
						if (text5 == "elite")
						{
							text += " (Elite=1) AND";
						}
					}
					else
					{
						text += " (OnTop=1) AND";
					}
				}
			}
			if (LinkType != -1)
			{
				text = text + " (LinkType=" + LinkType.ToString() + ") AND";
			}
			string str = Text.ReplaceBadChar(ArticleType);
			text = text + " (ArticleType='" + str + "') ";
			string text6;
			switch (SortType)
			{
			case 1:
				text6 = " ArticleID desc";
				break;
			case 2:
				text6 = " ArticleID asc";
				break;
			case 3:
				text6 = " InputTime desc";
				break;
			case 4:
				text6 = " InputTime asc";
				break;
			case 5:
				text6 = " UpdateTime desc";
				break;
			case 6:
				text6 = " UpdateTime asc";
				break;
			case 7:
				text6 = " Hits desc";
				break;
			case 8:
				text6 = " Hits asc";
				break;
			default:
				text6 = " ArticleID desc";
				break;
			}
			string text7 = "";
			if (Summary)
			{
				text7 = " summary,";
			}
			text = string.Concat(new string[]
			{
				"select top ",
				PageSize.ToString(),
				text7,
				" ArticleID,ShortTitle,LognTitle,LinkType,LinkUrl,InputTime,UpdateTime,Hits,Editor,UploadPicUrl,IsHighDisplay From HLShop_IndexArticle Where (Status=1) And ",
				text,
				" Order By ",
				text6
			});
			return this.conn.GetDataTable(text);
		}
	}
}
